;;(load "yexp.scm")
;;(load "key-aux.scm")

(def/err (cyl-key s n macro-str key-str)
  (let* ((res-str "")
	 (new-key "")
	 (res-cyl '())
	 (res-st #f)
	 (res-deli-str "")
	 (res-deli-st #f)
	 (st #f)
	 (deli-str "")
	 (define-func-name/flag #f)
	 (cyl-l '())
	 (cb-cyl-l '())
	 (fd-cyl-l '())
	 (str-l '())
	 (cb-str-l '())
	 (fd-str-l '())
	 (mk-str-l '())
	 (mk-cyl-l '())
	 (mk-deli-st #f)
	 (last-str "")
	 (last-cyl '())
	 (last-st #f)
	 (last-fn "")
	 (last-par-num 0)
	 (key-coding-l (find-system-dict-key-coding-list key-str))
	 (key-coding (nth 1 key-coding-l))
	 (key-handle (nth 7 (nth 2 key-coding-l)))
	 (deli-cyl '())
	 (key-travel2-list '("针对每个索引" "针对每个元素" "针对每个子项" "针对每组索引子项"))
	  )
     (pre-handle-key/key-aux)
     (tr-string (loc c s n)
	       ;;(y-or-n "CYL-tr-string:loc=~a\n" loc)
	       (cond
		((rd-p-deli c)
		 (let-set! (deli-str loc st) (get-deli s loc loc '开始))
		 ;;(if (and (string? last-str)(string=? last-str "下一字符")) (y-or-n "CYL-deli:deli-st=~a,cyl-l=~a,fd-cyl-l=~a,cb-cyl-l=~a.status=~a,loc=~a\nundefined-list=~a\n" st cyl-l fd-cyl-l cb-cyl-l cyl-define-status loc cyl-undefined-symbol-list))
		 ;;(y-or-n "key1:loc=~a,deli-str=~a,st=~a\n" loc deli-str st)
		 (cond
		  ((and (eq? st '函数定义分隔符)(eq? cyl-define-status 'func-define))
		   (run/err rewrite-cyl/pure-yexp->syntax mk-cyl-l)
		   (set! deli-cyl (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)))
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop mk-cyl-l) cyl-tl)
			 (push (pop mk-str-l) str-tl)
			 (push deli-cyl cyl-tl)
			 (push deli-str str-tl)
			 (handle-macro/key-aux cyl-tl str-tl)
			 )
		       (begin
			 (push deli-cyl mk-cyl-l)
			 (push deli-str mk-str-l)
			 ))
		   )
		  ((eq? st '函数定义分隔符)
		   (exit-cyl-key (list 'err loc (merge-num loc last-cyl 1) '非法语法 (err-format 'cyl-macro loc "函数定义分隔符不应该出现在系统关键字子句中，只允许在句首语句中出现。")))
		   )
		  ((eq? st '结束分隔符)
		   (run/err rewrite-cyl/pure-yexp->syntax mk-cyl-l)
		   (set! deli-cyl (list deli-str (string-length deli-str) deli-return-coding '结束分隔符 (cyl-serial)))
		   ;;(y-or-n "cyl-key:loc=~a,status=~a,deli=~a\nmk-cyl-l=~a\n" loc cyl-define-status deli-cyl mk-cyl-l)
		   (exit-cyl-key (list (merge-str loc mk-str-l) loc (merge-cyl loc mk-cyl-l '系统关键字调用) '系统关键字调用 deli-cyl #f key-handle))
		   )
		  ((eq? st '语句终结分隔符)
		   (run/err rewrite-cyl/pure-yexp->syntax mk-cyl-l)
		   (set! deli-cyl (list deli-str (string-length deli-str) deli-return-coding '语句终结分隔符 (cyl-serial)))
		   ;;(run/err make-key-cyl-list/key-aux)
		   (exit-cyl-key (list (merge-str loc mk-str-l) loc (merge-cyl loc mk-cyl-l '系统关键字调用) '系统关键字调用 deli-cyl #f key-handle))
		   )
		  ((eq? st '分隔符)
		   (run/err rewrite-cyl/pure-yexp->syntax mk-cyl-l)
		   (set! deli-cyl (list deli-str (string-length deli-str) deli-std-coding '分隔符 (cyl-serial)))
		   (if (string? cyl-adverb-value)
		       (let ((cyl-tl '())(str-tl '()))
			 (push (pop mk-cyl-l) cyl-tl)
			 (push (pop mk-str-l) str-tl)
			 (push deli-cyl cyl-tl)
			 (push deli-str str-tl)
			 (handle-macro/key-aux cyl-tl str-tl)
			 )
		       (begin
			 (push deli-cyl mk-cyl-l)
			 (push deli-str mk-str-l)
			 ))
		   )
		(else
		 ;;maybe not get here.
		 (handle-error s (list 'err loc (merge-num loc last-cyl 1) '非法语法 (err-format 'cyl-key loc "[~a]非法分隔符。" deli-str)))
		 )))
		(else
		 ;;(y-or-n "CYL1:loc=~a\n" loc)
		 (let-set!  (last-str loc ls rs last-cyl last-st last-fn last-par-num) (run/err yexp s loc 0 0))
		 ;;(y-or-n "key2:loc=~a,last-cyl=~a\n" loc last-cyl)
		   (cond
		    ((eq? last-st '系统宏)
		     (handle-macro/key-aux mk-cyl-l mk-str-l)
		     ;;(y-or-n "key-macro:loc=~a\n" loc)
		     )
		    ((eq? last-st '系统关键字)
		     (handle-key/key-aux)
		     )
		    (else
		     ;;(y-or-n "cyl-key:last-cyl=~a,cyl-key-travel2=~a.\n" last-cyl cyl-key-travel2)
		     (if cyl-key-travel2 (run/err rewrite-cyl/ciyu->macro-var))  ;;宏引导变量
		     (push last-cyl mk-cyl-l)
		     (push last-str mk-str-l))))
		))
    ;;handle after string traveling.
    
    (let ((loc (string-length s))
	  (cur-str "")
	  (cur-st (list "\n" 1 '(sys 11) '结束分隔符 (cyl-serial)))
	  )
      ;;(y-or-n "CYL_END:cyl-l=~a" cyl-l)
      (list (merge-str loc mk-str-l) loc (merge-cyl loc mk-cyl-l '系统关键字调用) '系统关键字调用 cur-st #f))))
      
	
	
		  
		     
		     
		   
		   
		   
		   
			 
		 
		 
   
